<?php

/**
 * API调用
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
define('SITE_PATH', getcwd());
define("APP_DEBUG", false);
define('APP_PATH', SITE_PATH . '/Home/');
define('THINK_PATH', SITE_PATH . '/ThinkPHP/');
define('CORE_PATH', THINK_PATH . 'Lib/'); // 系统核心类库目录
define('EXTEND_PATH', THINK_PATH . 'Extend/'); // 系统扩展目录
define('TMPL_PATH', SITE_PATH . '/api/Template/');
define('VENDOR_PATH', EXTEND_PATH . 'Vendor/');
define("RUNTIME_PATH", SITE_PATH . "/Runtime/");
define("LIB_PATH", APP_PATH . "Lib/");
define('RUNTIME_FILE', RUNTIME_PATH . "~api.php");
define("CONF_PATH",SITE_PATH."/api/");//api配置文件
define('DATA_PATH', RUNTIME_PATH.'Data/'); // 项目数据目录
define('LOG_PATH',  RUNTIME_PATH.'Logs/'); // 项目日志目录
define("_PHP_FILE_", "/");
$op = isset($_GET['m']) && trim($_GET['m']) ? trim($_GET['m']) : exit('Operation can not be empty');
if (!preg_match('/([^a-z_]+)/i', $op) && file_exists(SITE_PATH . '/api/' . ucwords($op) . 'Action.class.php')) {
    $Action = ucwords($op);
} else {
    exit('API handler does not exist');
}

if (!APP_DEBUG && is_file(RUNTIME_FILE)) {
    require RUNTIME_FILE;
} else {
    require CONF_PATH.'runtime.php';
    require THINK_PATH . 'Common/common.php';
    include APP_PATH . 'Common/common.php';
    $list = array(
        CORE_PATH . 'Core/Think.class.php',
        CORE_PATH . 'Core/Behavior.class.php',
        CORE_PATH . 'Core/Model.class.php', // 控制器类
        CORE_PATH . 'Core/Db.class.php', // 控制器类
    );
// 加载模式文件列表
    foreach ($list as $key => $file) {
        if (is_file($file))
            require_cache($file);
    }
    alias_import(include THINK_PATH . 'Conf/alias.php');
    set_error_handler(array('Think', 'appError'));
    set_exception_handler(array('Think', 'appException'));
    spl_autoload_register(array('Think', 'autoload'));
    $list = array(
        THINK_PATH . 'Common/functions.php', // 标准模式函数库
        CORE_PATH . 'Core/Action.class.php', // 控制器类
    );
    foreach ($list as $file) {
        if (is_file($file)) {
            require_cache($file);
        }
    }
    
    import("api." . $Action . "Action", SITE_PATH);
    Think::Start();
}

// 创建编译缓存
function build_runtime_cache($append = '') {
    // 生成编译文件
    $defs = get_defined_constants(TRUE);
    $content = '$GLOBALS[\'_beginTime\'] = microtime(TRUE);';
    $content .= array_define($defs['user']);
    // 读取核心编译文件列表
    $list = array(
        //CONF_PATH.'runtime.php',
        THINK_PATH . 'Common/common.php',
        APP_PATH . 'Common/common.php',
        CORE_PATH . 'Core/Think.class.php',
        CORE_PATH . 'Core/Behavior.class.php',
        CORE_PATH . 'Core/Model.class.php', // 控制器类
        CORE_PATH . 'Core/Db.class.php', // 控制器类
    );
    foreach ($list as $file) {
        $content .= compile($file);
    }
    C(include CONF_PATH . 'config.php');
    // 编译框架默认语言包和配置参数
    $content .= $append . "\nC(" . var_export(C(), true) . ');import("api.".$Action."Action", SITE_PATH);Think::Start();';
    file_put_contents(RUNTIME_FILE, strip_whitespace('<?php ' . $content));
}

?>